home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12984 / 12984.xpi / chrome / VideoDownloaderToolbar.jar / content / netmonitor.js < prev    next >
Text File  |  2010-01-29  |  9KB  |  305 lines

  1. if(!com) var com={};
  2. if(!com.VidBar) com.VidBar={};
  3.  
  4. com.VidBar.NetMonitor = function(repository, pref) {
  5.     try {
  6.         this.repository = repository;
  7.         this.pref = pref;
  8.         this.pref.registerObserver(["mediareq-extensions",
  9.                         "mediaweight-enabled", "mediaweight-threshold"], this);
  10.  
  11.         this.observerService = Components.classes["@mozilla.org/observer-service;1"]
  12.                 .getService(Components.interfaces.nsIObserverService);
  13.         this.observerService.addObserver(this, "http-on-modify-request", false);
  14.         this.observerService.addObserver(this, "http-on-examine-response",
  15.                 false);
  16.         this.observerService.addObserver(this, "quit-application", false);
  17.  
  18.         this.cacheService = Components.classes["@mozilla.org/network/cache-service;1"]
  19.                 .getService(Components.interfaces.nsICacheService);
  20.         this.httpCacheSession = this.cacheService.createSession("HTTP",
  21.                 Components.interfaces.nsICache.STORE_ANYWHERE,
  22.                 Components.interfaces.nsICache.STREAM_BASED);
  23.         this.httpCacheSession.doomEntriesIfExpired = false;
  24.  
  25.         this.updateReqExtensions();
  26.         this.updateMediaWeight();
  27.         this.typePattern = new RegExp("^(audio|video)/");
  28.     } catch (e) {
  29.         com.VidBar.__e("com.VidBar.NetMonitor.constructor: " + e);
  30.     }
  31. }
  32.  
  33. com.VidBar.NetMonitor.prototype = {
  34.     observe : function(subject, topic, data) {
  35.         if (topic == "quit-application") {
  36.             this.observerService.removeObserver(this, "http-on-modify-request");
  37.             this.observerService.removeObserver(this,
  38.                     "http-on-examine-response");
  39.             this.observerService.removeObserver(this, "quit-application");
  40.         } else if (topic == "http-on-modify-request") {
  41.             if (typeof Components == 'undefined')
  42.                 return;
  43.             this.monitorRequest(subject);
  44.         } else if (topic == "http-on-examine-response") {
  45.             if (typeof Components == 'undefined')
  46.                 return;
  47.             this.monitorResponse(subject);
  48.         }
  49.     },
  50.  
  51.     observePref : function(data) {
  52.         if (data == "mediareq-extensions")
  53.             this.updateReqExtensions();
  54.         if (data == "mediaweight-enabled" || data == "mediaweight-threshold")
  55.             this.updateMediaWeight();
  56.     },
  57.  
  58.     updateReqExtensions : function() {
  59.         com.VidBar.__d("com.VidBar.NetMonitor.updateReqExtensions");
  60.         var exts = this.pref.getMediaExtensions();
  61.         this.reqPattern = new RegExp("[/\\?&]([^/\\?&=]+\\.(" + exts
  62.                 + "))(?:$|\\?|&|/)");
  63.     },
  64.  
  65.     updateMediaWeight : function() {
  66.         com.VidBar.__d("com.VidBar.NetMonitor.updateMediaWeight");
  67.         this.minMediaSize = this.pref.getMinMediaSize();
  68.     },
  69.  
  70.     getWindowFromChannel : function(httpChannel) {
  71.         //com.VidBar.__d("com.VidBar.NetMonitor.getWindowFromChannel");
  72.         var wnd = null;
  73.         if (httpChannel.notificationCallbacks) {
  74.             try {
  75.                 var notif = httpChannel.notificationCallbacks
  76.                         .QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  77.                 wnd = notif.getInterface(Components.interfaces.nsIDOMWindow);
  78.             } catch (e) {
  79.             }
  80.         }
  81.         if (wnd == null && httpChannel.loadGroup
  82.                 && httpChannel.loadGroup.notificationCallbacks) {
  83.             try {
  84.                 var lgNotif = httpChannel.loadGroup.notificationCallbacks
  85.                         .QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  86.                 wnd = lgNotif.getInterface(Components.interfaces.nsIDOMWindow);
  87.             } catch (e) {
  88.             }
  89.         }
  90.         return wnd;
  91.     },
  92.  
  93.     monitorRequest : function(subject) {
  94.         //com.VidBar.__d("com.VidBar.NetMonitor.monitorRequest");
  95.         try {
  96.             var channel = subject
  97.                     .QueryInterface(Components.interfaces.nsIHttpChannel);
  98.             if (channel.requestMethod != "GET")
  99.                 return;
  100.             var request = subject
  101.                     .QueryInterface(Components.interfaces.nsIRequest);
  102.             var url = request.name;
  103.             //com.VidBar.__d("com.VidBar.NetMonitor requst: "+url);
  104.             var cacheEntryDescriptor = this.httpCacheSession.openCacheEntry(
  105.                     url, Components.interfaces.nsICache.ACCESS_READ, false);
  106.             if (cacheEntryDescriptor
  107.                     && (cacheEntryDescriptor.accessGranted & 1)) {
  108.                 var headers = cacheEntryDescriptor
  109.                         .getMetaDataElement("response-head");
  110.                 if (/Location:/i.test(headers)) {
  111.                     cacheEntryDescriptor.close();
  112.                     return;
  113.                 }
  114.  
  115.                 var contentType = null;
  116.                 try {
  117.                     contentType = /Content-Type: *(.*)/i.exec(headers)[1];
  118.                 } catch (e) {
  119.                 }
  120.                 var contentLength = null;
  121.                 try {
  122.                     contentLength = /Content-Length: *(.*)/i.exec(headers)[1];
  123.                 } catch (e) {
  124.                 }
  125.                 var contentDisp = null;
  126.                 try {
  127.                     contentDisp = /Content-Disposition: *(.*)/i.exec(headers)[1];
  128.                 } catch (e) {
  129.                 }
  130.                 cacheEntryDescriptor.close();
  131.  
  132.                 var wnd = this.getWindowFromChannel(channel);
  133.                 this.analyze(url, contentType, contentDisp, contentLength, wnd);
  134.             }
  135.         } catch (e) {
  136.             //com.VidBar.__e("com.VidBar.NetMonitor.monitorRequest: " + e);
  137.         }
  138.     },
  139.  
  140.     monitorResponse : function(subject) {
  141.         //com.VidBar.__d("com.VidBar.NetMonitor.monitorResponse");
  142.         try {
  143.             var request = subject
  144.                     .QueryInterface(Components.interfaces.nsIRequest);
  145.  
  146.             var mediaUrl = request.name;
  147.             var httpChannel = subject
  148.                     .QueryInterface(Components.interfaces.nsIHttpChannel);
  149.  
  150.             var contentType = null;
  151.             try {
  152.                 contentType = httpChannel.getResponseHeader("content-type");
  153.             } catch (e) {
  154.             }
  155.             var contentLength = null;
  156.             try {
  157.                 contentLength = httpChannel.getResponseHeader("content-length");
  158.             } catch (e) {
  159.             }
  160.             var contentDisp = null;
  161.             try {
  162.                 contentDisp = httpChannel
  163.                         .getResponseHeader("content-disposition");
  164.             } catch (e) {
  165.             }
  166.  
  167.             this.analyze(mediaUrl, contentType, contentDisp, contentLength,
  168.                     this.getWindowFromChannel(httpChannel));
  169.         } catch (e) {
  170.             com.VidBar.__e("com.VidBar.NetMonitor.monitorResponse: " + e);
  171.         }
  172.     },
  173.  
  174.     analyze : function(mediaUrl, contentType, contentDisp, contentLength, wnd) {
  175.         //com.VidBar.__d("com.VidBar.NetMonitor.analyze");
  176.         var hit = false;
  177.  
  178.         if ((contentType != null && this.typePattern.test(contentType))
  179.                 || (this.reqPattern.test(mediaUrl))) {
  180.  
  181.             if (contentLength != null && isNaN(contentLength) == false
  182.                     && contentLength < this.minMediaSize)
  183.                 return;
  184.  
  185.             var names = this.getFileName(mediaUrl, contentType, contentDisp,
  186.                     wnd);
  187.             var filename = names[0];
  188.             var extension = names[1];
  189.  
  190.             try {
  191.                 var doc = null;
  192.                 var pageUrl = null;
  193.                 var Name = null;
  194.                 var labelFile = filename;
  195.                 var nameFile = filename;
  196.                 if (wnd != null && wnd.document) {
  197.                     doc = wnd.document;
  198.                     pageUrl = doc.URL;                    
  199.                     if (this.pref.getFileNameType() == "title") {
  200.                         if(doc.title != null) {
  201.                             nameFile = doc.title;
  202.                             nameFile = nameFile.replace(/[^`a-zA-Z0-9\-]/g, "_");
  203.                             if (nameFile.length > 80)
  204.                                 nameFile = nameFile.substring(1,80);
  205.                             if(nameFile > 25) {
  206.                                 labelFile = nameFile.substring(1,20);
  207.                                 labelFile = labelFile + "..." + extension;
  208.                             } else
  209.                                 labelFile = nameFile + "." + extension;        
  210.                             nameFile = nameFile +"." + extension;    
  211.                         }
  212.                     }
  213.                     
  214.                 }
  215.  
  216.                 var data = {
  217.                     type : "network",
  218.                     label : labelFile,
  219.                     filename : nameFile,
  220.                     extension : extension,
  221.                     pageUrl : pageUrl,
  222.                     mediaUrl : mediaUrl,
  223.                     referrer : pageUrl,
  224.                     size : contentLength
  225.                 };
  226.  
  227.                 com.VidBar.__d("com.VidBar.NetMonitor.analyze\ndoc: " + pageUrl + "\nfilename: " + filename);
  228.  
  229.                 var rep = this.repository;
  230.                 setTimeout(function() {
  231.                             rep.addMediaListByDoc([data], doc);
  232.                         }, 0, null);
  233.             } catch (e) {
  234.                 com.VidBar.__e("com.VidBar.NetMonitor.analyze: " + e);
  235.             }
  236.         }
  237.     },
  238.  
  239.     getFileName : function(mediaUrl, contentType, contentDisp, wnd) {
  240.         com.VidBar.__d("com.VidBar.NetMonitor.getFileName");
  241.         var filename = null;
  242.         var extension = null;
  243.  
  244.         if (contentDisp != null) {
  245.             if (/filename=/.test(contentDisp)) {
  246.                 filename = /filename="?([^;"]*)/.exec(contentDisp)[1];
  247.                 try {
  248.                     extension = /.*\.(.*?)$/.exec(filename)[1];
  249.                 } catch (e) {
  250.                     extension = "";
  251.                 }
  252.             }
  253.         }
  254.         if (filename == null) {
  255.             if (contentType != null && /^video\/x-.*$/.test(contentType)) {
  256.                 extension = /video\/x-([^;]*)/.exec(contentType)[1];
  257.             } else if (contentType != null && /^video\/.+$/.test(contentType)) {
  258.                 extension = /video\/([^ ,]*).*$/.exec(contentType)[1];
  259.             } else if (contentType != null && /^audio\/.+$/.test(contentType)) {
  260.                 extension = /audio\/(?:x-)?([^ ,]*).*?$/.exec(contentType)[1];
  261.             } else {
  262.                 if (/^[^\?]*\.[0-9a-zA-Z]{1,5}$/.test(mediaUrl))
  263.                     extension = /\.([0-9a-zA-Z]{1,5})$/.exec(mediaUrl)[1];
  264.                 else
  265.                     extension = "flv";
  266.             }
  267.             var re = new RegExp("([^/&\\?]+\\." + extension + ")(?:$|&|\\?)");
  268.             if (re.test(mediaUrl)) {
  269.                 var m = re.exec(mediaUrl);
  270.                 filename = m[1];
  271.             } else if (this.reqPattern.test(mediaUrl)) {
  272.                 var m = this.reqPattern.exec(mediaUrl);
  273.                 filename = m[1];
  274.                 extension = m[2];
  275.             } else {
  276.                 try {
  277.                     var title = null;
  278.                     if (wnd) {
  279.                         title = Util.xpGetString(wnd.document.documentElement,
  280.                                 "/html/head/meta[@name='title']/@content");
  281.                         if (title == null || title == "")
  282.                             title = Util.xpGetString(
  283.                                     wnd.document.documentElement,
  284.                                     "/html/head/title");
  285.                     }
  286.                     if (title == null || title == "")
  287.                         title = "file-"
  288.                                 + Math.floor(Math.random() * 1000000000);
  289.                     filename = title.replace(/[^a-zA-Z0-9-]+/g, "_") + "."
  290.                             + extension;
  291.                 } catch (e) {
  292.                     filename = "file-" + Math.floor(Math.random() * 1000000000)
  293.                             + "." + extension;
  294.                 }
  295.             }
  296.             if (filename.length > 64) {
  297.                 var parts = /^(.*)(\..*?)$/.exec(filename);
  298.                 filename = parts[1].substr(0, 64 - parts[2].length) + parts[2];
  299.             }
  300.         }
  301.  
  302.         return [filename, extension];
  303.     }
  304. };
  305.